package com.ruangong.GitClient.app.YanZhaoWang;


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;


import android.content.Intent;

import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

import com.ruangong.GitClient.R;
import com.ruangong.GitClient.YanZhaoWang.*;

public class MajorListDisplay extends AppCompatActivity {

    private ListView searchResultListView;
    private ArrayAdapter<Major> adapter;
    private List<Major> searchResults;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_major_list_display);

        searchResultListView = findViewById(R.id.websiteListView);

        // 初始化搜索结果列表
        searchResults = new ArrayList<>();
        adapter = new MajorAdapter(this, searchResults);

        searchResultListView.setAdapter(adapter);
        Intent intent = getIntent();
        int ssdm = intent.getIntExtra("selectedProvince", -1);
        String university= intent.getStringExtra("selectedUniversity");
        String yixkdm = intent.getStringExtra("selectedSubject");
        // 执行搜索操作
        performGetMajor(ssdm,yixkdm,university);
       // performGetMajor(11,yixkdm,university);
        // 设置列表项的点击事件
        searchResultListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取点击的项的内容
                Major clickedMajor = searchResults.get(position);
                // 创建跳转到另一个Activity的意图
                Intent intent = new Intent(MajorListDisplay.this, MajorDetails.class);
                // 在Intent中传递额外的数据，如果需要的话
                // intent.putExtra("key", value);
                // 启动另一个Activity
                intent.putExtra("MAJOR", clickedMajor);
                startActivity(intent);
            }
        });
    }

    // 执行搜索操作
    private void performGetMajor(int ssdm,String yixkdm,String university) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                // 在这里执行网络请求

                List<Major> majors = YanZhaoWang.getMajorList(ssdm,yixkdm,university);
                searchResults.clear();
                if (majors == null || majors.isEmpty()) {
                    searchResults.add(new Major("","", "很抱歉，没有找到您要搜索的数据", "","", "", "", "", "")); // 添加一个标记项
                    searchResultListView.setOnItemClickListener(null);
                } else {
                    // 将搜索结果添加到 searchResults 列表中
                    for (Major link : majors) {
                        searchResults.add(link);
                    }
                }
                // 切换回主线程更新UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 更新UI
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();




    }
}